package com.example.administrator.mail_list;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements IOnDelListener,ClickViewListener{
    private RecyclerView recyclerList;
    private ArrayList<Student> studentArrayList = new ArrayList<>();
    //搜索结果列表
    private ArrayList<Student> searchResultList = new ArrayList<>();
    private StuAdapter adapter;
    private Button addStu;
    private EditText editKey;
    private Button searchStu;
    private Boolean isResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initStuList();

        initRecyclerView();

        initAdd();

        initSearch();
    }

    //搜索
    private void initSearch() {
        editKey = findViewById(R.id.editKey);
        searchStu = findViewById(R.id.btn_search);

        searchStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String key = editKey.getText().toString();
                search(studentArrayList,key);
                adapter.changeData(searchResultList);
                isResult = true;
            }
        });

        //搜索框自动搜索
        editKey.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String key = editKey.getText().toString();
                search(studentArrayList,key);
                adapter.changeData(searchResultList);
                isResult = true;
            }
        });
    }

    //搜索判断
    private void search(ArrayList<Student> studentArrayList, String key) {
        //每次搜索，都得清空结果列表
        searchResultList.clear();

        for(int i=0; i<studentArrayList.size(); i++){
            Student s = studentArrayList.get(i);

            if(s.getName().contains(key) || String.valueOf(s.getAge()).equals(key)){
                searchResultList.add(s);
            }

        }
    }

    //添加事件
    private void initAdd() {
        addStu = findViewById(R.id.btn_add);
        addStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,AddActivity.class);
                startActivityForResult(intent,1001);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        Log.e("MainActivity", "requestCode = "+ requestCode +"resultCode = "+ resultCode);

        if(data == null){
            return;
        }

        if(resultCode == 2001){
            String number = data.getStringExtra("NUMBER");
            String name = data.getStringExtra("NAME");
            int age = data.getIntExtra("AGE",0);
            int imgId = data.getIntExtra("IMG",R.drawable.s1);

            Student s = new Student(number,imgId,name,age);
            studentArrayList.add(s);

            adapter.notifyDataSetChanged();
        }else if(resultCode == 3001){
            String number = data.getStringExtra("NUMBER");
            String name = data.getStringExtra("NAME");
            int age = data.getIntExtra("AGE",0);
            int imgId = data.getIntExtra("IMG",R.drawable.s1);

            for(int i=0; i<studentArrayList.size(); i++){
                Student s = studentArrayList.get(i);

                if(s.getNumber().equals(number)){
                    s.setAge(age);
                    s.setName(name);
                    s.setImgId(imgId);
                }
            }

            //编辑过后重新搜索一次，若不同，则会更新结果列表
            String key = editKey.getText().toString();
            search(studentArrayList,key);
            adapter.changeData(searchResultList);
            isResult = true;
        }
    }

    //初始化RecyclerView
    private void initRecyclerView() {
        recyclerList = findViewById(R.id.recyclerList);
        adapter = new StuAdapter(MainActivity.this,studentArrayList,
                MainActivity.this,MainActivity.this);
        recyclerList.setAdapter(adapter);

        //LayoutManager指定RecyclerView的布局方式，让其呈现类似ListView的效果
        //否则页面出不来
        LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
        recyclerList.setLayoutManager(manager);
    }

    //初始化学生列表
    private void initStuList() {
        Student s1 = new Student("1001",R.drawable.s1,"小一",18);
        Student s2 = new Student("1002",R.drawable.s2,"小二",19);
        Student s3 = new Student("1003",R.drawable.s3,"小三",20);
        Student s4 = new Student("1004",R.drawable.s4,"小四",21);
        Student s5 = new Student("1005",R.drawable.s5,"小五",22);

        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);
    }

    //删除
    @Override
    public void del(String number) {
        for(int i=0; i<studentArrayList.size(); i++){
            if(studentArrayList.get(i).getNumber().equals(number)){
                studentArrayList.remove(i);
            }
        }

        for(int i=0; i<searchResultList.size(); i++) {
            if (searchResultList.get(i).getNumber().equals(number)) {
                searchResultList.remove(i);
            }
        }

        adapter.notifyDataSetChanged();
    }

    //recyclerView的点击事件
    @Override
    public void click(View view,final int i) {
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Student s = studentArrayList.get(i);

                if(isResult){
                    s = searchResultList.get(i);
                }

                Toast.makeText(MainActivity.this,
                        "姓名："+ s.getName() + "    年龄："+ s.getAge(),
                        Toast.LENGTH_SHORT).show();

                Intent intent = new Intent();
                //将数据传递到下一个活动
                intent.putExtra("NUMBER",s.getNumber());
                intent.putExtra("NAME",s.getName());
                intent.putExtra("AGE",s.getAge());
                intent.putExtra("IMG",s.getImgId());

                intent.setClass(MainActivity.this,EditActivity.class);
                startActivityForResult(intent,1002);
            }
        });
    }
}
